<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head >
    <title>添加新数据</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
    <script>
        var index = 0;
        try{
            index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        }catch (e) {
        }
    </script>
</head>
<body >

<div class="layui-body" style="padding-top: 20px;">
    <style>
        input[type="file"] {
            appearance: none;
            background-color: initial;
            cursor: default;
            align-items: baseline;
            color: inherit;
            text-overflow: ellipsis;
            white-space: pre;
            text-align: start !important;
            padding: initial;
            border: initial;
            overflow: hidden !important;
        }
    </style>
    <div class="sp-wrapper">
        <div class="order-body">

            <form method="post" class="layui-form" id="sp-form">

                <div class="layui-form-item">
                    <label class="layui-form-label">来源：</label>
                    <div class="layui-input-block">
                        <input type="radio" name="source" value="淘宝" title="淘宝" >
                        <input type="radio" name="source" value="拼多多" title="拼多多" checked>
                        <input type="radio" name="source" value="抖音" title="抖音" >
                        <input type="radio" name="source" value="服装趋势网" title="服装趋势网" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">类型：</label>
                    <div class="layui-input-block">
                        <input type="radio" name="type" value="SITE" title="网站" >
                        <input type="radio" name="type" value="GOODS" title="商品" checked>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="required" placeholder="标题" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">日期</label>
                    <div class="layui-inline">
                        <input type="text" name="date" id="date" lay-verify="required" th:value="${date}" placeholder="日期" autocomplete="off" class="layui-input" style="width: 120px;">
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <label class="layui-form-label">图片</label>
                    <div class="layui-input-inline">
                            <img id="imgV" style="height: 100px;">
                            <input type="file" onchange="addImage(this)" class="layui-input">
                            <input type="hidden" name="img" id="img" value="">
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <label class="layui-form-label">链接URL</label>
                    <div class="layui-input-block">
                        <input type="text" name="url"  placeholder="链接URL" autocomplete="off" class="layui-input">
                    </div>
                </div>
                
            
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                        <input type="text" name="remark" placeholder="" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="add_btn">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    <script type="text/javascript" src="/sb/jquery.min.js"></script>
    <script type="text/javascript" src="/sb/Tips.min.js"></script>

    <!-- end sp-wrapper -->
    <script>
        layui.use(['laydate', 'form','layer'], function () {
            var laydate = layui.laydate;
            var form = layui.form;
            var layer = layui.layer;

            //执行一个laydate实例
            laydate.render({
                elem: '#date' //指定元素
            });
   

            form.on('submit(add_btn)', function(data){
                console.log(data.field);
                $.ajax({
                    type:"POST",
                    url:"/ecom/goods_style_refer_add_ajax",
                    processData: false, //表明不要对数据进行处理
                    data:JSON.stringify(data.field),
                    contentType: 'application/json',
                    success:function (res) {
                        if(res.code == 0){
                            parent.layer.msg(res.msg)
                            parent.layer.close(index);
                            parent.location.reload();
                        }else {
                            layer.msg(res.msg)
                        }
                        return false;
                    }
                });
                return false;
            });
        });

        function addImage(file) {
            var fd = new FileReader();
            var img = file.previousSibling;
            var imgInput = file.nextSibling;
            fd.readAsDataURL(file.files[0]);
            fd.onload = function (e) {
                $("#imgV").attr('src',this.result);
                $("#imgV").show();
                $("#img").val(this.result);
                //console.log(this.result)
                // imgInput.value =this.result;
            };
        }

    </script>
      </div>
    </div>
</div>
</body>
<html>
